<?php
namespace Tianqi\Controller;
use Common\Model\CityModel;
use Common\Model\InfoContentModel;
use Common\Model\NewsModel;
use Common\Controller\BehaviorController;
class IndexController extends BehaviorController {

    public function index(){
        $cid = I('get.id');
        if(!$cid){
            $cid = cookie('defaultCityId');
        }
        if(!$cid){
            $cid = C('default_weather_city_id');
        }

        $caheKey = sprintf('weather_city_%s',$cid);
        $weather = F($caheKey);
        if(!$weather){
            $weather = $this->getWeatherById($cid);
        }
        $weather = json_decode($weather,true);
        //错误判断
        if($weather['HeWeather data service 3.0'][0]['status'] == 'invalid key'){
            exit('错误的用户 key');
        }else if($weather['HeWeather data service 3.0'][0]['status'] == 'unknown city'){
            exit('未知城市');
        }else if($weather['HeWeather data service 3.0'][0]['status'] == 'no more requests'){
            exit('	超过访问次数');
        }else if($weather['HeWeather data service 3.0'][0]['status'] == 'anr'){
            exit('服务无响应或超时');
        }else if($weather['HeWeather data service 3.0'][0]['status'] == 'permission denied'){
            exit('没有访问权限');
        }
        //直辖市
        $cityModel = new CityModel();
        $cityList =$cityModel->getCityListByProv('直辖市');
        $HBcityList =$cityModel->getCityListByProv('湖北');

        //推荐信息
        $infoModel = new InfoContentModel();
        $hotInfo = $infoModel->getHotInfo();

        //热门新闻
        $newsModel = new NewsModel();
        $hotNews = $newsModel->getHotNews();
        $this->assign('weather',$weather['HeWeather data service 3.0'][0])
            ->assign('cityList',$cityList)
            ->assign('hotInfo',$hotInfo)
            ->assign('hotNews',$hotNews)
            ->assign('HBcityList',$HBcityList);

        $this->display('/weather/index');
    }

    public function getWeatherById($id){
        $key = C('weather_api_key');
        $cityModel = new CityModel();
        $cityId = $cityModel->find($id);
        if($cityId){
            $url = sprintf('https://api.heweather.com/x3/weather?cityid=%s&key=%s',$cityId['id'],$key);
            $data = curl($url);
            if($data){
                $caheKey = sprintf('weather_city_%s',$id);
                F($caheKey,$data);
                return $data;
            }
        }else{
            return array();
        }
    }

    public function plugin(){
        $cid = I('get.id');

        if(!$cid){
            $cid = cookie('defaultCityId');
        }
        if(!$cid){
            $cid = C('default_weather_city_id');
        }

        $caheKey = sprintf('weather_city_%s',$cid);
        $weather = F($caheKey);
        if(!$weather){
            $weather = $this->getWeatherById($cid);
        }
        $weather = json_decode($weather,true);
        //错误判断
        if($weather['HeWeather data service 3.0'][0]['status'] != 'ok'){
            exit('天气加载失败,请稍后重试');
        }
        $weekarray=array("日","一","二","三","四","五","六");
        $limitDays = I('days') ? I('days') : 2;
        $this->assign('weather',$weather['HeWeather data service 3.0'][0])
                ->assign('id',$cid)
                ->assign('limitDays',$limitDays)
                ->assign('weekarray',$weekarray);
        $this->display('/weather/them_1');
    }
//    public function test(){
//
//        $model = new CityModel();
//        $city = $model->field('cid,city')->order('cid asc')->select();
//        $city = array_column($city,'cid','city');
//        $temp = array();
//        foreach($city as $k => $v){
//            $key = urlencode($k);
//            $temp[$key] = $v;
//        }
//        $data = json_encode($temp);
//        $data = urldecode($data);
//        $str = <<<EOF
//var city = $data;
//EOF;
//    file_put_contents('./city.js',$str);
//    }





}